<template>
  <div class="app-main-content-box">
    <el-form
      ref="form"
      v-loading="loading"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="small"
    >
      <el-form-item label="启用积分商城：">
        <el-switch v-model="form.mallState" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round @click="onSubmit()">保 存</el-button>
        <el-button round @click="resetData()">重 置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getPointsMallConfig, updatePointsMallConfig } from '@/api/points'
export default {
  props: {
    tabName: {
      type: String,
      require: true
    }
  },
  data() {
    return {
      /**
         * 表单校验字段相关
         */
      rules: {},
      // 状态
      loading: false,
      // 数据
      form: {
        mallState: 0
      }
    }
  },
  watch: {
    tabName(val) {
      if (val === 'config') {
        this.initData()
      }
    }
  },
  methods: {
    /**
       * 获取数据
       */
    // 获取配置
    initData() {
      this.loading = true
      getPointsMallConfig()
        .then(res => {
          this.form.mallState = res.data.mallState
          // if (res.data.mallState===1){
          //   this.form.mallState=true
          // } else {
          //   this.form.mallState=false
          // }
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 功能开启与否
    //  handeSwitch(val){
    //    if (val){
    //     this.form.mallState=true
    //    } else {
    //      this.form.mallState=false
    //    }
    //  },
    /**
       *  按钮组
       */
    // 保存
    onSubmit() {
      this.loading = true
      const updateForm = {}
      // if (this.form){
      //     updateForm.mallState=1
      // } else {
      //    updateForm.mallState=0
      // }
      updatePointsMallConfig(this.form)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 重置
    resetData() {
      this.form.mallState = !this.form.mallState
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
